package bb.lanxing.lib.devices.utils.scan;

import bb.lanxing.lib.kt.ByteArrayExtKt;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.UUID;
import kotlin.Metadata;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;
import kotlin.jvm.internal.StringCompanionObject;
import kotlin.text.Charsets;
import android.util.Log;

/* compiled from: ParseBluetoothAdData.kt */

public final class ParseBluetoothAdData {
    public static final Companion Companion = new Companion(null);
    private static final String TAG = "ParseBluetoothAdData";

    public static final class Companion {
        public Companion(DefaultConstructorMarker defaultConstructorMarker) {
            this();
        }

        public final int byteToUnsignInt(byte b) {
            return b < 0 ? b + 256 : b;
        }

        private Companion() {
        }

        protected static final char[] hexArray = "0123456789ABCDEF".toCharArray();

        public static String bytesToHex(byte[] bArr) {
            char[] cArr = new char[(bArr.length * 3) - 1];
            for (int i = 0; i < bArr.length; i++) {
                int i2 = bArr[i] & 255;
                int i3 = i * 3;
                char[] cArr2 = hexArray;
                cArr[i3] = cArr2[i2 >>> 4];
                cArr[i3 + 1] = cArr2[i2 & 15];
                if (i != bArr.length - 1) {
                    cArr[i3 + 2] = '-';
                }
            }
            return new String(cArr);
        }

        public final AdData parse(byte[] param1ArrayOfbyte) {
            String str1Appearance = null;
            String str2Name = null;
            Intrinsics.checkNotNullParameter(param1ArrayOfbyte, "bytes");
            ArrayList<UUID> arrayListUUID = new ArrayList();
            ByteBuffer byteBuffer = ByteBuffer.wrap(param1ArrayOfbyte).order(ByteOrder.LITTLE_ENDIAN);
            byte[] arrayOfByte1Name = null;
            byte[] arrayOfByte2Manufacturer = null;
            byte[] arrayOfByte3ServiceData = null;
            param1ArrayOfbyte = null;
            while (byteBuffer.remaining() > 2) {
                int i = byteBuffer.get() & 0xFF;     //i is Data All Length
                if (i == 0)
                    break;
                int k = byteBuffer.get() & 0xFF;    //k is Data Type
                int j = --i;                        //j is Data Value Length
                if (k != 20) {               //0x14    List of 16-bit Service Solicitation UUIDs
                    int m = i;
                    if (k != 21) {           //0x15    List of 128-bit Service Solicitation UUIDs
                        if (k != 25) {       //0x19    Appearance
                            if (k != 255) {  //0xFF    Manufacture Specific Data
                                int n = i;
                                m = i;
                                j = i;
                                switch (k) {
                                    default:
                                        byteBuffer.position(byteBuffer.position() + i);
                                        continue;
                                    case 8:   //0x08    Shortened Local Name
                                    case 9:   //0x09    Complete Local Name
                                        arrayOfByte1Name = new byte[i];
                                        byteBuffer.get(arrayOfByte1Name);
                                        String hexStringName = bytesToHex(arrayOfByte1Name);
                                        //Log.d(TAG, "arrayOfByte1Name HEX is " + hexStringName );
                                        //str2Name = new String(arrayOfByte1Name, Charsets.UTF_8);
                                        continue;
                                    case 22:   //0x16    Service Data
                                        arrayOfByte3ServiceData = new byte[i];
                                        byteBuffer.get(arrayOfByte3ServiceData);
                                        //String hexStringServiceData = bytesToHex(arrayOfByte3ServiceData);
                                        //Log.d(TAG, "arrayOfByte3ServiceData HEX is " + hexStringServiceData );
                                        continue;
                                    case 4:  //0x04    Incomplete List of 32-bit Service Class UUIDs
                                    case 5:  //0x05    Complete List of 32-bit Service Class UUIDs
                                        while (n >= 4) {
                                            StringCompanionObject stringCompanionObject = StringCompanionObject.INSTANCE;
                                            String str = String.format("%08X-0000-1000-8000-00805F9B34FB", Arrays.copyOf(new Object[] {byteBuffer.getInt()}, 1));
                                            Intrinsics.checkNotNullExpressionValue(str, "format(format, *args)");
                                            arrayListUUID.add(UUID.fromString(str));
                                            n -= 4;
                                        }
                                        continue;
                                    case 1:  //0x01    Flags
                                        byteBuffer.position(byteBuffer.position() + i);
                                        continue;
                                    case 6:  //0x06    Incomplete List of 128-bit Service Class UUIDs
                                    case 7:  //0x07    Complete List 128-bit Service Class UUIDs
                                        while (m >= 16) {
                                            long l = byteBuffer.getLong();
                                            arrayListUUID.add(new UUID(byteBuffer.getLong(), l));
                                            m -= 16;
                                        }
                                        continue;
                                    case 2:  //0x02    Incomplete List of 16-bit Service Class UUIDs
                                    case 3:  //0x03    Complete List of 16-bit Service Class UUIDs
                                        break;
                                }
                            } else {
                                arrayOfByte2Manufacturer = new byte[i];
                                byteBuffer.get(arrayOfByte2Manufacturer, 0, i);

                                //String hexStringManufacturer = bytesToHex(arrayOfByte2Manufacturer);
                                //Log.d(TAG, "arrayOfByte2Manufacturer HEX is " + hexStringManufacturer );
                                continue;
                            }
                        } else {
                            param1ArrayOfbyte = new byte[i];
                            byteBuffer.get(param1ArrayOfbyte);
                            str1Appearance = printlnByteArrayToUnsignHexString(param1ArrayOfbyte);
                            continue;
                        }
                    } else {

                    }
                }
                while (j >= 2) {
                    StringCompanionObject stringCompanionObject = StringCompanionObject.INSTANCE;
                    String str = String.format("%08X-0000-1000-8000-00805F9B34FB", Arrays.copyOf(new Object[] { Short.valueOf(byteBuffer.getShort()) }, 1));
                    Intrinsics.checkNotNullExpressionValue(str, "format(format, *args)");
                    arrayListUUID.add(UUID.fromString(str));
                    j -= 2;
                }
            }
            return new AdData(arrayListUUID, arrayOfByte2Manufacturer, str2Name, str1Appearance,arrayOfByte3ServiceData);
        }
        /*public final AdData parse(byte[] bytes) {
            int i;
            Intrinsics.checkNotNullParameter(bytes, "bytes");
            ArrayList arrayList = new ArrayList();
            ByteBuffer order = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
            byte[] bArr = null;
            String str = null;
            String str2 = null;
            while (order.remaining() > 2 && (i = order.get() & 255) != 0) {
                int i2 = order.get() & 255;
                int i3 = i - 1;
                if (i2 != 20) {
                    if (i2 != 21) {
                        if (i2 == 25) {
                            byte[] bArr2 = new byte[i3];
                            order.get(bArr2);
                            str2 = printlnByteArrayToUnsignHexString(bArr2);
                        } else if (i2 != 255) {
                            switch (i2) {
                                case 1:
                                    order.position(order.position() + i3);
                                    break;
                                case 2:
                                case 3:
                                    break;
                                case 4:
                                case 5:
                                    while (i3 >= 4) {
                                        StringCompanionObject stringCompanionObject = StringCompanionObject.INSTANCE;
                                        String format = String.format("%08X-0000-1000-8000-00805F9B34FB", Arrays.copyOf(new Object[]{order.getInt()}, 1));
                                        Intrinsics.checkNotNullExpressionValue(format, "format(format, *args)");
                                        arrayList.add(UUID.fromString(format));
                                        i3 -= 4;
                                    }
                                    break;
                                case 6:
                                case 7:
                                    break;
                                case 8:
                                case 9:
                                    byte[] bArr3 = new byte[i3];
                                    order.get(bArr3);
                                    str = new String(bArr3, Charsets.UTF_8);
                                    break;
                                default:
                                    order.position(order.position() + i3);
                                    break;
                            }
                        } else {
                            bArr = new byte[i3];
                            order.get(bArr, 0, i3);
                        }
                    }
                    while (i3 >= 16) {
                        arrayList.add(new UUID(order.getLong(), order.getLong()));
                        i3 -= 16;
                    }
                }
                while (i3 >= 2) {
                    StringCompanionObject stringCompanionObject2 = StringCompanionObject.INSTANCE;
                    String format2 = String.format("%08X-0000-1000-8000-00805F9B34FB", Arrays.copyOf(new Object[]{Short.valueOf(order.getShort())}, 1));
                    Intrinsics.checkNotNullExpressionValue(format2, "format(format, *args)");
                    arrayList.add(UUID.fromString(format2));
                    i3 -= 2;
                }
            }
            return new AdData(arrayList, bArr, str, str2);
        }*/

        public final String printlnByteArrayToUnsignHexString(byte[] bArr) {
            if (bArr == null) {
                return null;
            }
            return ByteArrayExtKt.readStringLE(bArr, 0, bArr.length, "hex", false);
        }

        public static final class AdData {
            private final ArrayList<UUID> UUIDs;
            private String appearance;
            private final byte[] manufacturerByte;
            private final byte[] servicedataByte;
            private final String name;

            public AdData(ArrayList<UUID> UUIDs, byte[] bArr, String str, String str2, byte[] bArr2) {
                Intrinsics.checkNotNullParameter(UUIDs, "UUIDs");
                this.UUIDs = UUIDs;
                this.manufacturerByte = bArr;
                this.name = str;
                this.appearance = str2;
                this.servicedataByte = bArr2;
            }

            public final String getAppearance() {
                return this.appearance;
            }

            public final byte[] getManufacturerByte() {
                return this.manufacturerByte;
            }

            public final byte[] getServicedataByte() {
                return this.servicedataByte;
            }

            public final String getName() {
                return this.name;
            }

            public final ArrayList<UUID> getUUIDs() {
                return this.UUIDs;
            }

            public final void setAppearance(String str) {
                this.appearance = str;
            }
        }
    }
}
